package cn.jasonone.jasonfx;

import java.util.List;

import cn.jasonone.jasonfx.annotation.FXScanner;
import cn.jasonone.jasonfx.factory.BeanBuilder;
import cn.jasonone.jasonfx.factory.BeanFactory;
import cn.jasonone.jasonfx.factory.ControllerFactory;
import cn.jasonone.jasonfx.manager.WindowManager;
import cn.jasonone.jasonfx.util.Resources;
import cn.jasonone.jasonfx.util.StringUtil;
import javafx.application.Application;
import javafx.stage.Stage;

public class JasonFxApplication extends Application {
	private static final String FRAMWORK_ROOT_PACKAGE = "cn.jasonone.jasonfx";
	private static Class<?> applicationClass;

	@Override
	public void start(Stage primaryStage) throws Exception {
		// 获取扫描包名称,默认为applicationClass所在包及其子包
		Package classPackage = applicationClass.getPackage();
		String packageName = classPackage == null ? "" : classPackage.getName();
		if (applicationClass.isAnnotationPresent(FXScanner.class)) {
			FXScanner annotation = applicationClass.getAnnotation(FXScanner.class);
			if (!StringUtil.isEmpty(annotation.value())) {
				packageName = annotation.value();
			}
		}
		// 初始化框架
		// 扫描框架的包
		List<Class<?>> classes = Resources.scaner(FRAMWORK_ROOT_PACKAGE);
		// 扫描用户项目包
		classes.addAll(Resources.scaner(packageName));
		//注册控制器
		ControllerFactory.register(classes);
		//初始化窗口管理器
		WindowManager.init();
		// 批量注册控制器
//        ControllerManager.registers(classes);
		// 批量注册绑定处理器
//        BindManager.registers(classes);
		// 批量绑定控制器
//        BindManager.binds(ControllerManager.getControllers());
		// 批量注册处理器
//        HandlerManager.registers(classes);
		// 批量注册舞台
//        StageManager.registers(classes);

	}
	
	public static void start(Class<?> applicationClass,BeanBuilder builder,String...args) {
		JasonFxApplication.applicationClass = applicationClass;
		BeanFactory.setBuilder(builder);
		JasonFxApplication.launch(args);
	}

	public static void start(Class<?> applicationClass, String... args) {
		start(applicationClass, new BeanBuilder() {}, args);
	}

}
